#include "base/cpuinfo.h"
#include "base/eventloop.h"
#include "base/eventloopthread.h"
#include "base/log.h"

#include "base/eventloopthreadpool.h"
namespace net_tools{

Eventloopthreadpool::Eventloopthreadpool(eventloop_func func,int thread_number,int channel_size)
  :func_(std::move(func)),
   thread_number_(thread_number),
   index(0),
   eventlooppool_()
{
  if(!thread_number_){ //NULL AUTO
    base::Cpuinfo::Loadcpuinfo();
    thread_number_ = base::Cpuinfo::cpu_core_number_ - 1;
  }
  if(thread_number_ < 1){
    thread_number_ = 1;
  }
  NT_LOG_INFO << "thread number " << thread_number_ << NT_LOG_ENDL;
  std::string name = "IOthread_";
  for(int count = 0;count < thread_number_;++count){
    std::string count_string = std::to_string(count);
    std::string thread_name = name+count_string;
    net_tools::Eventloopthread* eventloopthread = new net_tools::Eventloopthread(func_,channel_size,thread_name);
    eventlooppool_.push_back(eventloopthread->Get_eventloop_());
  }
};
net_tools::Eventloop* Eventloopthreadpool::Get_next_eventloop(){
  ++index;
  if(index == thread_number_){
    index = 0;
  }
  return eventlooppool_[index];
}
int Eventloopthreadpool::Get_thread_number() const {
  return thread_number_;
};
std::vector<net_tools::Eventloop*>* Eventloopthreadpool::Get_ptr_looppool(){
  return &eventlooppool_;
};

} // namespace net_tools